package com.punai.dataanalysis.epdatakb.controller;

import cn.hutool.core.collection.CollUtil;
import com.punai.common.bos.KVBO;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.utils.spring.SpringUtils;
import com.punai.dataanalysis.epdatakb.bo.NG001.SjxyfxBasic;
import com.punai.dataanalysis.epdatakb.constants.DataKbConstants;
import com.punai.dataanalysis.epdatakb.service.DataAnalysisParentService;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.constants.Ng001Constants;
import com.punai.system.service.IEcoCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/dataAnalysis/epdatakb")
public class DataAnalysisDataKbController extends BaseController {

    @Autowired
    private IEcoCompanyService ecoCompanyService;

    @GetMapping("/getTableDataList")
    public AjaxResult getTableDataList(DataAnalysisVo vo) {
        String enumType = vo.getEnumType();
        String tabType = vo.getTabType();
        Class<? extends DataAnalysisParentService> actuator = DataKbConstants.DATA_ANALYSYS_ENUM_TABS_TYPE.getActuator(enumType, tabType);
        DataAnalysisParentService bean = SpringUtils.getBean(actuator);
        List<? extends BaseEntity> tabsDataList = bean.getDataList(vo);
        return AjaxResult.success(tabsDataList);
    }


    @GetMapping("/getShowCompanyList")
    public AjaxResult getShowCompanyList() {
        Ng001Constants.CompanyOrderEnum[] values = Ng001Constants.CompanyOrderEnum.values();
        List<KVBO> kvbos = new ArrayList<>();
        for (Ng001Constants.CompanyOrderEnum value : values) {
            KVBO kvbo = new KVBO();
            kvbo.setKey(value.getType());
            kvbo.setValue(value.getType());
            kvbos.add(kvbo);
        }
        return AjaxResult.success(kvbos);
    }

    @GetMapping("/getEcoCompanyList")
    public AjaxResult getEcoCompanyList() {
        EcoCompany ecoCompany = new EcoCompany();
        // 正常的分公司
        ecoCompany.setStatus("0");
        List<EcoCompany> ecoCompanies = ecoCompanyService.selectCompanyList(ecoCompany);
        List<KVBO> kvbos = new ArrayList<>();
        if (CollUtil.isNotEmpty(ecoCompanies)) {
            ecoCompanies.forEach(item -> item.setKeyNameOrder(Ng001Constants.CompanyOrderEnum.getOrderForType(item.getCompanyName())));
            // 按照Order字段进行排序
            ecoCompanies.sort(Comparator.comparingInt(EcoCompany::getKeyNameOrder));
            for (EcoCompany value : ecoCompanies) {
                KVBO kvbo = new KVBO();
                kvbo.setKey(value.getCompanyName());
                kvbo.setValue(value.getCompanyName());
                kvbos.add(kvbo);
            }
        }
        return AjaxResult.success(kvbos);
    }

}
